﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;


namespace Photography.Admin
{
    public partial class SendEmail : System.Web.UI.Page
    {
        public Email Email { get; private set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!(Master as Photography.Admin.AdminMaster).AdminID.HasValue)
                Response.Redirect("Default.aspx");

            Email = new Email(MapPath("EmailTemplate.htm"), MapPath("Header.png"), "Header.png");
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            MailAddressCollection toEmails = new MailAddressCollection();
            foreach (ListItem item in lstContacts.Items)
                if (item.Selected)
                    toEmails.Add(item.Value);
            Email.Send(toEmails, txtSubject.Text, txtEmailBody.Text);
            lblSuccessful.Visible = true;
        }

        protected void GetContacts(object sender, LinqDataSourceSelectEventArgs e)
        {
            Photography.App_Data.PhotographyDataContext context = new Photography.App_Data.PhotographyDataContext();
            e.Result = context.GetContacts();
        }

        protected void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (ListItem item in lstContacts.Items)
                item.Selected = true;
        }

        protected void btnDeselectAll_Click(object sender, EventArgs e)
        {
            foreach (ListItem item in lstContacts.Items)
                item.Selected = false;
        }
    }
}